package com.serverTechnology.teamsGroups.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.gettyio.core.util.ObjectUtil;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.serverTechnology.teamsGroups.entity.ProModel;
import com.serverTechnology.teamsGroups.entity.TeWarehouse;
import com.serverTechnology.teamsGroups.entity.dto.TeWarehouseAddEditDto;
import com.serverTechnology.teamsGroups.entity.vo.TeWarehouseEditVo;
import com.serverTechnology.teamsGroups.mapper.TeWarehouseMapper;
import com.serverTechnology.teamsGroups.service.ITeShelfService;
import com.serverTechnology.teamsGroups.service.ITeWarehouseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * <p>
 * 仓库 服务实现类
 * </p>
 *
 * @author 王凯炫
 * @since 2024-08-06
 */
@Service
public class TeWarehouseServiceImpl extends ServiceImpl<TeWarehouseMapper, TeWarehouse> implements ITeWarehouseService {
    @Resource
    private TeWarehouseMapper teWarehouseMapper;

    @Resource
    private ITeShelfService teShelfService;
    /**
     * 添加修改仓库
     * @param teWarehouseAddEditDto
     */
    @Override
    public void warehouseadd(TeWarehouseAddEditDto teWarehouseAddEditDto) {
        TeWarehouse teWarehouse = new TeWarehouse();
        BeanUtils.copyProperties(teWarehouseAddEditDto,teWarehouse);
        try {
            // 使用三元运算符简化保存或更新逻辑
            boolean b = ObjectUtils.isNull(teWarehouseAddEditDto.getId()) ? this.save(teWarehouse) : this.updateById(teWarehouse);
        } catch (Exception e) {
            throw new RuntimeException("编码重复");
        }
    }

    /**
     * 根据id查询仓库
     * @param id
     * @return
     */
    @Override
    public TeWarehouseEditVo getByIds(Long id) {
        MPJLambdaWrapper<TeWarehouse> wrapper = new MPJLambdaWrapper<>();
        wrapper.eq(TeWarehouse::getId,id)
                .selectAll(TeWarehouse.class)
                .select(ProModel::getModelName)
                .leftJoin(ProModel.class, ProModel::getId, TeWarehouse::getProductLineModelId)
                .selectAs(ProModel::getModelName,TeWarehouseEditVo::getProductLineModelName);
        TeWarehouseEditVo teWarehouseEditVo = teWarehouseMapper.selectJoinOne(TeWarehouseEditVo.class, wrapper);
        return teWarehouseEditVo;
    }
}
